package com.mobilezu.mobile.pushserver.console.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.xmpp.packet.Presence;
import com.mobilezu.mobile.pushserver.console.vo.SessionVO;
import com.mobilezu.mobile.pushserver.model.PushUser;
import com.mobilezu.mobile.pushserver.service.PushUserService;
import com.mobilezu.mobile.pushserver.service.ServiceManager;
import com.mobilezu.mobile.pushserver.xmpp.session.ClientSession;
import com.mobilezu.mobile.pushserver.xmpp.session.Session;
import com.mobilezu.mobile.pushserver.xmpp.session.SessionManager;


/**
 * һ���������࣬�����?ǰ�ĵ��������ߵ��û�
 * 
 * @author ��С��(xiaodao.co@gmail.com) 2012-8-20
 * @link http://xiaodao.co �� http://chenxiaodao.sinaapp.com
 * @QQ 525396193
 * @version 0.6.3
 */
public class SessionController extends MultiActionController {

	private PushUserService pushUserService;

	public SessionController() {
		pushUserService = ServiceManager.getPushUserService();
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ClientSession[] sessions = new ClientSession[0];
		sessions = SessionManager.getInstance().getSessions().toArray(sessions);

		List<SessionVO> voList = new ArrayList<SessionVO>();
		for (ClientSession sess : sessions) {
			SessionVO vo = new SessionVO();
			String sessionUsername = sess.getUsername();
			vo.setUsername(sessionUsername);

			PushUser user = pushUserService
					.getPushUserByUsername(sessionUsername);
			String name = user.getName();
			if (user != null && name != null) {
				vo.setName(name);
			} else {
				vo.setName(sessionUsername);
			}

			vo.setResource(sess.getAddress().getResource());

			// Status
			if (sess.getStatus() == Session.STATUS_CONNECTED) {
				vo.setStatus("CONNECTED");
			} else if (sess.getStatus() == Session.STATUS_AUTHENTICATED) {
				vo.setStatus("AUTHENTICATED");
			} else if (sess.getStatus() == Session.STATUS_CLOSED) {
				vo.setStatus("CLOSED");
			} else {
				vo.setStatus("UNKNOWN");
			}
			// Presence
			if (!sess.getPresence().isAvailable()) {
				vo.setPresence("Offline");
			} else {
				Presence.Show show = sess.getPresence().getShow();
				if (show == null) {
					vo.setPresence("Online");
				} else if (show == Presence.Show.away) {
					vo.setPresence("Away");
				} else if (show == Presence.Show.chat) {
					vo.setPresence("Chat");
				} else if (show == Presence.Show.dnd) {
					vo.setPresence("Do Not Disturb");
				} else if (show == Presence.Show.xa) {
					vo.setPresence("eXtended Away");
				} else {
					vo.setPresence("Unknown");
				}
			}
			vo.setClientIP(sess.getHostAddress());
			vo.setCreatedDate(sess.getCreationDate());
			voList.add(vo);
		}

		ModelAndView mav = new ModelAndView();
		mav.addObject("sessionList", voList);
		mav.setViewName("session/session_list");
		return mav;
	}
}
